package com.mail.dubbo.api;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.mapper.PmsProductAttributeMapper;
import com.mail.dubbo.mapper.PmsProductCategoryAttributeRelationMapper;
import com.mail.dubbo.mapper.PmsProductCategoryMapper;
import com.mail.model.db.PmsProductCategory;
import com.mail.model.db.PmsProductCategoryAttributeRelation;
import com.mail.model.dto.ProductCategoryDto;
import com.mail.model.vo.ChildrenCategoryVo;
import com.mail.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class PmsProductCategoryApiImpl implements PmsProductCategoryApi{

    @Autowired
    private PmsProductCategoryMapper productCategoryMapper;
    @Autowired
    private PmsProductAttributeMapper productAttributeMapper;
    @Autowired
    private PmsProductCategoryAttributeRelationMapper productCategoryAttributeRelationMapper;

    // 添加商品分类
    @Override
    public void createCategory(ProductCategoryDto productCategoryDto) {
        // 首先将接收到的信息存入分类表
        PmsProductCategory pmsProductCategory = new PmsProductCategory();
        BeanUtils.copyProperties(productCategoryDto,pmsProductCategory);
        productCategoryMapper.insert(pmsProductCategory);
        // 获取添加数据的主键
        Long categoryId = pmsProductCategory.getId();
        // 将手动选择的规格或参数存入关系表
        List<Integer> productAttributeIdList = productCategoryDto.getProductAttributeIdList();
        for (Integer integer : productAttributeIdList) {
            PmsProductCategoryAttributeRelation productCategoryAttributeRelation = new PmsProductCategoryAttributeRelation();
            productCategoryAttributeRelation.setProductAttributeId(Long.valueOf(integer));
            productCategoryAttributeRelation.setProductCategoryId(categoryId);
            productCategoryAttributeRelationMapper.insert(productCategoryAttributeRelation);
        }
    }

    // 删除商品分类
    @Override
    public void deleteCategory(Integer categoryId) {
        // 首先删除分类表中的数据
        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductCategory::getId,Long.valueOf(categoryId));
        productCategoryMapper.delete(wrapper);
        // 再删除添加的规格或参数关系表对应的数据
        LambdaQueryWrapper<PmsProductCategoryAttributeRelation> relationWrapper = new LambdaQueryWrapper<>();
        relationWrapper.eq(PmsProductCategoryAttributeRelation::getProductCategoryId,Long.valueOf(categoryId));
        productCategoryAttributeRelationMapper.delete(relationWrapper);
    }

    // 查询所有一级分类及子分类
    @Override
    public List<ChildrenCategoryVo> queryWithChildrenCategory() {
        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        // 查询所有以及分类
        wrapper.eq(PmsProductCategory::getLevel,0);
        List<PmsProductCategory> pmsProductCategories = productCategoryMapper.selectList(wrapper);
        List<ChildrenCategoryVo> childrenCategoryVos = new ArrayList<>();
        for (PmsProductCategory pmsProductCategory : pmsProductCategories) {
            ChildrenCategoryVo childrenCategoryVo = new ChildrenCategoryVo();
            LambdaQueryWrapper<PmsProductCategory> childrenWrapper = new LambdaQueryWrapper<>();
            childrenWrapper.eq(PmsProductCategory::getParentId,pmsProductCategory.getId());
            List<PmsProductCategory> childrenCategoryList = productCategoryMapper.selectList(childrenWrapper);
            BeanUtils.copyProperties(pmsProductCategory,childrenCategoryVo);
            childrenCategoryVo.setChildren(childrenCategoryList);
            childrenCategoryVos.add(childrenCategoryVo);
        }
        return childrenCategoryVos;
    }

    // 分页查询商品分类
    @Override
    public Page<PmsProductCategory> queryCategoryList(Integer parentId, Integer pageNum, Integer pageSize) {
        Page<PmsProductCategory> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductCategory::getParentId,Long.valueOf(parentId));
        wrapper.orderByDesc(PmsProductCategory::getSort);
        Page<PmsProductCategory> pmsProductCategoryPage = productCategoryMapper.selectPage(pageInfo, wrapper);
        return pmsProductCategoryPage;
    }

    // 修改导航栏显示状态
    @Override
    public void changeNavStatus(List<Integer> ids, Integer navStatus) {
        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        // 将int集合转换为long集合
        List<Long> listLong = JSONArray.parseArray(ids.toString(),Long.class);
        wrapper.in(PmsProductCategory::getId,listLong);
        PmsProductCategory pmsProductCategory = new PmsProductCategory();
        pmsProductCategory.setNavStatus(navStatus);
        productCategoryMapper.update(pmsProductCategory,wrapper);
    }

    // 修改显示状态
    @Override
    public void changeShowStatus(List<Integer> ids, Integer showStatus) {
        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        // 将int集合转换为long集合
        List<Long> listLong = JSONArray.parseArray(ids.toString(),Long.class);
        wrapper.in(PmsProductCategory::getId,listLong);
        PmsProductCategory pmsProductCategory = new PmsProductCategory();
        pmsProductCategory.setShowStatus(showStatus);
        productCategoryMapper.update(pmsProductCategory,wrapper);
    }

    // 修改商品分类
    @Override
    public void changeCategory(Integer id, ProductCategoryDto productCategoryDto) {
        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductCategory::getId,Long.valueOf(id));
        PmsProductCategory pmsProductCategory = new PmsProductCategory();
        BeanUtils.copyProperties(productCategoryDto,pmsProductCategory);
        // 修改商品分类
        productCategoryMapper.update(pmsProductCategory,wrapper);

        // 首先删除原先的关系
        LambdaQueryWrapper<PmsProductCategoryAttributeRelation> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(PmsProductCategoryAttributeRelation::getProductCategoryId,Long.valueOf(id));
        productCategoryAttributeRelationMapper.delete(wrapper1);

        List<Integer> productAttributeIdList = productCategoryDto.getProductAttributeIdList();
        for (Integer integer : productAttributeIdList) {
            PmsProductCategoryAttributeRelation pmsProductCategoryAttributeRelation = new PmsProductCategoryAttributeRelation();
            pmsProductCategoryAttributeRelation.setProductAttributeId(Long.valueOf(integer));
            pmsProductCategoryAttributeRelation.setProductCategoryId(Long.valueOf(id));
            // 再存入更新的数据
            productCategoryAttributeRelationMapper.insert(pmsProductCategoryAttributeRelation);
        }

    }

    // 根据id获取商品分类
    @Override
    public PmsProductCategory queryCategoryById(Long id) {
        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductCategory::getId,id);
        PmsProductCategory pmsProductCategory = productCategoryMapper.selectOne(wrapper);
        return pmsProductCategory;
    }
}

